const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;
const taskService = require("../../../apiservice/task/taskservice");

// pages/tasks/search/search.js
Page({
	data: {
		pageSize: 20,
		height: '',
		condition: '',
		pagenum: 0,
		displaytotal: 0,
		isnextPage: false,
		searchStr: ""
	},

	async GetSearhPage(condition, pageNum, pageSize) {
		try {
			var result = await taskService.getTaskByNameOrPhineOrLandline(condition, pageNum, pageSize);
			if (result.data && result.data && result.data.datas) {
				var displaytotal = this.data.displaytotal + result.data.datas.length;
				this.setData({
					condition: condition,
					pagenum: pageNum,
					displaytotal: displaytotal,
					isnextPage: result.data.existsnextdata,
					total: result.data.total,
					['list[' + (pageNum - 1) + ']']: result.data.datas
				});
				console.log(this.data);
				if (result.data.datas.length == 0) {
					wx.showModal({
					  title: '提示',
					  showCancel: false,
					  content: '未找到相关数据，请重新查询',
					})
				}
			}
		} catch (e) {
			//TODO handle the exception
		} finally {
			wx.hideLoading();
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function(options) {
		var res = wx.getSystemInfoSync();
		this.setData({
			height: res.windowHeight
		});

	},

	searchTakChange: async function(e) {
		wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
			title: '加载中',
			icon: 'loading'
		});
		var condition = e.detail.searchKey;
		var pageNum = 1;
		var pageSize = this.data.pageSize;
		//debugger;
		this.setData({
			['list']: null,
			isnextPage: false
		})
		await this.GetSearhPage(condition, pageNum, pageSize);

	},

	lower: async function() {
		//debugger;
		if (this.data.isnextPage) {
			wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
				title: '加载中',
				icon: 'loading'
			});
			var pageNum = this.data.pagenum;
			var condition = this.data.condition;
			var pageSize = this.data.pageSize;
			await this.GetSearhPage(condition, ++pageNum, pageSize);
			//wx.hideLoading();

		} else {
			wx.showToast({ //如果全部加载完成了也弹一个框
				title: '我也是有底线的',
				icon: 'success',
				duration: 350
			});
			return false;
		}
	},

	cancelClick: function(e) {
		console.log(e.detail.taskId);
		var that = this;
		wx.showModal({
			title: '提示',
			content: '您确定要取消任务吗？',
			success: function(sm) {
				if (sm.confirm) {
					console.log(e)
					that.cancelTask(e.detail.taskId);
				} else if (sm.cancel) {
					console.log('用户点击取消')
				}
			}
		})
	},

	cancelTask: function(taskId) {
		var entityName = "task";
		var state = 2;
		var status = 6;
		taskService.UpdateState(taskId, entityName, state, status).then(function(result) {
			console.log(result);
			wx.switchTab({
				url: "../list/list",
				success: function(e) {
					var page = getCurrentPages().pop();
					console.log(getCurrentPages());
					if (!page) return;
					page.onLoad();
				}
			});
		}).catch(function(error) {

		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
